package repositories;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;

import specifications.ClientSpecification;
import dbHelper.DBHelper;
import entities.Client;


public class ClientRepository implements Repository<Client> {

	public ArrayList<Client> ClientList;
	
	
	public ClientRepository()
	{
		this.ClientList = null;
	}
	
	@Override
	public void Add(Client Client) {
		try {
			DBHelper.addObject(Client);
		} catch (NoSuchMethodException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException
				| SQLException e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public void Delete(Client Client) {
		try {
			DBHelper.deleteObject(Client);
		} catch (IllegalAccessException | IllegalArgumentException
				| InvocationTargetException | NoSuchMethodException
				| SecurityException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void Update(Client Client) {
		try {
			DBHelper.updateObject(Client);
		} catch (NoSuchMethodException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void FindAll() {//throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, SQLException, InstantiationException {
		Client Cl = new Client();

		try {
			try {
				this.ClientList = (ArrayList<Client>) dbHelper.DBHelper.findObjects(Cl);
			} catch (NoSuchMethodException | SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (IllegalAccessException | IllegalArgumentException
				| InvocationTargetException | InstantiationException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	public ArrayList<Client> findBySpecification (ClientSpecification spec) {
		if(this.ClientList == null)
			this.FindAll();
		ArrayList<Client> res = new ArrayList<Client>();
		for (Client el : ClientList)
		{
			if (spec.isSatisfiedBy(el))
			{
				res.add(el);
			}
		}
		return res;
	}


}
